<template>
	<xn-form-container
		:title="formData.customerId ? '编辑客户' : '增加客户'"
		:width="700"
		v-model:open="open"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="vertical">
			<a-row :gutter="16">
				<a-col :span="12">
					<a-form-item label="客户编号：" name="customerCode">
						<a-input v-model:value="formData.customerCode" placeholder="请输入客户编号" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="客户名称：" name="customerName">
						<a-input v-model:value="formData.customerName" placeholder="请输入客户名称" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="业务员：" name="receiverIdList">
						<xn-user-selector
							:org-tree-api="selectorApiFunction.orgTreeApi"
							:user-page-api="selectorApiFunction.userPageApi"
							:user-list-by-id-list-api="selectorApiFunction.userListByIdListApi"
							data-type="object"
							:add-show="true"
							:radio-model="true"
							v-model:value="formData.supplierSalesPerson"
						/>
					</a-form-item>


				</a-col>
				<a-col :span="12">
					<a-form-item label="联系人：" name="customerContacts">
						<a-input v-model:value="formData.customerContacts" placeholder="请输入联系人" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="联系电话：" name="customerContactNumber">
						<a-input v-model:value="formData.customerContactNumber" placeholder="请输入联系电话" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="单位地址：" name="customerWorkAddress">
						<a-input v-model:value="formData.customerWorkAddress" placeholder="请输入单位地址" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="邮编：" name="customerPostalCode">
						<a-input v-model:value="formData.customerPostalCode" placeholder="请输入邮编" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="期初欠款：" name="customerOpeningDebt">
						<a-input v-model:value="formData.customerOpeningDebt" placeholder="请输入期初欠款" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="开户银行：" name="customerOpeningBank">
						<a-input v-model:value="formData.customerOpeningBank" placeholder="请输入开户银行" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="银行账号：" name="customerAccountNumber">
						<a-input v-model:value="formData.customerAccountNumber" placeholder="请输入银行账号" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="纳税号：" name="customerTaxId">
						<a-input v-model:value="formData.customerTaxId" placeholder="请输入纳税号" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="公司主页：" name="customerCompanyHomepage">
						<a-input v-model:value="formData.customerCompanyHomepage" placeholder="请输入公司主页" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="电子邮箱：" name="customerEmail">
						<a-input v-model:value="formData.customerEmail" placeholder="请输入电子邮箱" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="公司法人：" name="customerLegalPerson">
						<a-input v-model:value="formData.customerLegalPerson" placeholder="请输入公司法人" allow-clear />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="备注：" name="remarks">
						<a-textarea v-model:value="formData.remarks" placeholder="请输入备注" :auto-size="{ minRows: 3, maxRows: 5 }" />
					</a-form-item>
				</a-col>
				<a-col :span="12">
					<a-form-item label="状态：" name="status">
						<a-select v-model:value="formData.status" placeholder="请选择状态" :options="statusOptions" />
					</a-form-item>
				</a-col>
			</a-row>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="imsCustomerForm">
	import tool from '@/utils/tool'
	import { cloneDeep } from 'lodash-es'
	import { required } from '@/utils/formRules'
	import imsCustomerApi from '@/api/ims/imsCustomerApi'
	import XnUserSelector from "@/components/XnUserSelector/index.vue";
	import bizOrgApi from "@/api/biz/bizOrgApi";
	import userCenterApi from "@/api/sys/userCenterApi";
	// 抽屉状态
	const open = ref(false)
	const emit = defineEmits({ successful: null })
	const formRef = ref()
	// 表单数据
	const formData = ref({})
	const submitLoading = ref(false)
	const statusOptions = ref([])

	// 打开抽屉
	const onOpen = (record) => {
		open.value = true
		if (record) {
			let recordData = cloneDeep(record)
			formData.value = Object.assign({}, recordData)
		}
		statusOptions.value = tool.dictList('COMMON_STATUS')
		formData.value.status = statusOptions.value[0].value
	}
	// 关闭抽屉
	const onClose = () => {
		formRef.value.resetFields()
		formData.value = {}
		open.value = false
	}
	// 默认要校验的
	const formRules = {
		customerCode: [required('请输入客户编号')],
		customerName: [required('请输入客户名称')],
		status: [required('请输入状态')],
	}
	// 验证并提交数据
	const onSubmit = () => {
		formRef.value
			.validate()
			.then(() => {
				submitLoading.value = true

				if (formData.value.supplierSalesPerson && formData.value.supplierSalesPerson.length > 0) {
					formData.value.supplierSalesPerson = formData.value.supplierSalesPerson[0]
				}

				const formDataParam = cloneDeep(formData.value)
				imsCustomerApi
					.imsCustomerSubmitForm(formDataParam, formDataParam.customerId)
					.then(() => {
						onClose()
						emit('successful')
					})
					.finally(() => {
						submitLoading.value = false
					})
			})
			.catch(() => {})
	}
	const selectorApiFunction = {
		orgTreeApi: (param) => {
			return bizOrgApi.orgTreeSelector(param).then((data) => {
				return Promise.resolve(data)
			})
		},
		userPageApi: (param) => {
			return bizOrgApi.orgUserSelector(param).then((data) => {
				return Promise.resolve(data)
			})
		},
		userListByIdListApi: (param) => {
			return userCenterApi.userCenterGetUserListByIdList(param).then((data) => {
				return Promise.resolve(data)
			})
		}
	}


	// 抛出函数
	defineExpose({
		onOpen
	})
</script>
